package ldh.im.fxbase.util;

import javafx.application.HostServices;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import javafx.stage.Screen;
import javafx.stage.Stage;
import ldh.rpc.RpcBootstrap;

import java.awt.*;

public class StageUtil {

    private static volatile Stage stage;
    private static volatile Stage mainStage;
    private static volatile StackPane root;
    private static volatile HostServices HostServices;

    public static void setStage(Stage stage) {
        StageUtil.stage = stage;
    }

    public static Stage getStage() {
        return stage;
    }

    public static void setMainStage(Stage stage) {
        StageUtil.mainStage = stage;
    }

    public static Stage getMainStage() {
        return mainStage;
    }

    public static void close() {
        if(stage != null) {
            stage.close();
            RpcBootstrap rpcBootstrap = RpcUtil.getRpcBootstrap();
            if(rpcBootstrap != null) {
                rpcBootstrap.close();
            }
            if(isSupportTray()) {
                System.exit(0);
            }
        }
    }

    public static boolean isMac() {
        return System.getProperty( "os.name", "" ).toLowerCase().contains( "mac" );
    }

    public static boolean isWindow() {
        String osName =  System.getProperty( "os.name", "" );
        return osName.toLowerCase().contains("windows");
    }

    public static boolean isLinux() {
        String osName = System.getProperty( "os.name", "" ).toLowerCase();
        return osName.contains("nix") || osName.contains("nux");
    }

    public static boolean isSupportTray() {
        return SystemTray.isSupported();
    }

    public static StackPane getRoot() {
        return root;
    }

    public static void setRoot(StackPane root) {
        StageUtil.root = root;
    }

    public static javafx.application.HostServices getHostServices() {
        return HostServices;
    }

    public static void setHostServices(javafx.application.HostServices hostServices) {
        HostServices = hostServices;
    }

    public static double calcWidth(String str) {
        final Text text = new Text(str);
        new Scene(new Group(text));
        text.applyCss();
        final double width = text.getLayoutBounds().getWidth();
        return width;
    }

    public static double getWindowWidth() {
        Screen screen = Screen.getPrimary();
        return screen.getVisualBounds().getWidth();
    }

    public static double getWindowHeight() {
        Screen screen = Screen.getPrimary();
        return screen.getVisualBounds().getHeight();
    }
}
